
// ************************* INCLUDES **************************

#include "target.h"
#include "kernel_FreeRTOS.h"

#include "service_scheduling.h"
#include "..\config\service_scheduling_config.h"

// *********************** DEFINITIONS *************************

#define NUMBER_OF_TASKS		(sizeof(tasksConfig)/sizeof(tasksConfig[0]))

typedef service_scheduling_TASK_FUNCTION_DECLARATION((*TaskFunctionType), void);

typedef struct {
	TaskFunctionType taskFunction;
	const char *taskName;
	U16 taskStackSize;
	U8 taskPriority;
	portTickType taskPeriod;
} TaskConfigType;

// ******************** GLOBAL VARIABLES ***********************

CODE_MEMORY TaskConfigType tasksConfig[] = TASKS_CONFIG;

portTickType tasksLastWakeTime[NUMBER_OF_TASKS];

// *************************** BODY ****************************

static void service_scheduling_taskEntry(U32 taskIndex)
{
	tasksLastWakeTime[taskIndex] = xTaskGetTickCount();

	while(TRUE)
	{
		vTaskDelayUntil(&tasksLastWakeTime[taskIndex], tasksConfig[taskIndex].taskPeriod);

		tasksConfig[taskIndex].taskFunction();
	}
}

void service_scheduling_init(void)
{
	U32 i;

	for(i = 0; i < NUMBER_OF_TASKS; i++)
	{
		xTaskCreate((pdTASK_CODE)service_scheduling_taskEntry, (const signed char *)tasksConfig[i].taskName, tasksConfig[i].taskStackSize, (void*)(i), tasksConfig[i].taskPriority, NULL);
	}
}

void service_scheduling_executeSystem(void)
{
	vTaskStartScheduler();
}
